import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { UserEntity } from '@/db/entitys/user.entity';
import { ConfigService } from '@nestjs/config';

@Injectable({ scope: Scope.REQUEST })
export class ReqProvider {
  constructor(
    @Inject(REQUEST) private readonly _req: Request,
    private readonly configService: ConfigService,
  ) {}

  get user(): UserEntity | undefined {
    return this._req.user as UserEntity | undefined;
  }

  get req(): Request {
    return this._req;
  }

  get domain(): string {
    const req = this._req;
    // return `${req.protocol}://${req.get('host')}${req.originalUrl}`
    return `${this._req.protocol || 'http'}://${this._req.get('host')}`;
  }

  getFullAssetUrl(relFilepath: string) {
    const staticPrefix = this.configService.get('STATIC_PREFIX', 'static');
    return `${this.domain}/${staticPrefix}/${relFilepath}`;
  }
}
